package com.alibaba.citrus.cr.price.ability.functions.entity.activity;

import com.alibaba.citrus.cr.price.repository.ActivityQueryRepository;
import com.alibaba.citrus.cr.price.repository.ActivityWriteRepository;
import com.alibaba.citrus.cr.common.util.SpringContextUtil;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.crforecastsales.activityuserecordrelation.dto.QueryActivityUseRecordRelationResponse;
import com.epoch.app.crpricecenter.dto.response.ActivityUseDetailViewResponse;
import com.epoch.app.crpricecenter.sdo.ActivityUseRecordRelationSDO;
import lombok.Data;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author hanguo
 * @date 2021/10/18
 * @Description 活动使用实体
 */
@Data
public class ActivityUseRecordRelationEntity {

    private ActivityUseRecordRelationSDO activityUseRecordRelationSDO;

    private static ActivityQueryRepository activityQueryRepository = SpringContextUtil.getBean(ActivityQueryRepository.class);

    private static ActivityWriteRepository activityWriteRepository = SpringContextUtil.getBean(ActivityWriteRepository.class);


    /**
     * 创建活动使用详情entity
     *
     * @param activityUseRecordRelationSDO
     * @return
     */
    public static ActivityUseRecordRelationEntity of(ActivityUseRecordRelationSDO activityUseRecordRelationSDO) {
        ActivityUseRecordRelationEntity activityUseRecordRelationEntity = new ActivityUseRecordRelationEntity();
        activityUseRecordRelationEntity.setActivityUseRecordRelationSDO(activityUseRecordRelationSDO);
        return activityUseRecordRelationEntity;
    }

    public Result<List<ActivityUseDetailViewResponse>> loadPromotionUsedDetailPageList() {
        QueryActivityUseRecordRelationResponse queryActivityUseRecordRelationResponse = activityQueryRepository.queryPromotionUsedDetailPageList(activityUseRecordRelationSDO);
        if(Objects.isNull(queryActivityUseRecordRelationResponse) || CollectionUtils.isEmpty(queryActivityUseRecordRelationResponse.getResult())){
            return Result.success(new ArrayList<>());
        }

        List<ActivityUseDetailViewResponse> collect = queryActivityUseRecordRelationResponse.getResult().stream().map(item -> {
            ActivityUseDetailViewResponse activityUseDetailViewResponse = new ActivityUseDetailViewResponse();
            //activityUseDetailViewResponse.setOrderId(item.getMainOrderId());
            //activityUseDetailViewResponse.setLineId(item.getSubOrderId());
            activityUseDetailViewResponse.setItemName(item.getItemName());
            activityUseDetailViewResponse.setItemCount(item.getItemQuantity() == null ? null : item.getItemQuantity().toString());
            activityUseDetailViewResponse.setSalesUnit(item.getSalesUnit());
            activityUseDetailViewResponse.setInventoryUnit(item.getStockUnit());
            activityUseDetailViewResponse.setConversionUnit(item.getConversionUnit());
            String features = item.getFeatures();
            if(StringUtils.isNotBlank(features)){
                activityUseDetailViewResponse.setOrderId((String) JSONObject.parseObject(features).get("forecastOrderBizId"));
            }
//            activityUseDetailViewResponse.setFeatures(item.getFeatures());
            //未知映射关系字段
            activityUseDetailViewResponse.setInventoryArea(null);
            activityUseDetailViewResponse.setInventoryAddress(null);

            return activityUseDetailViewResponse;
        }).collect(Collectors.toList());

        Result<List<ActivityUseDetailViewResponse>> result = Result.success(collect);
        result.setTotal(queryActivityUseRecordRelationResponse.getTotal());
        return result;
    }

    public Boolean deleteActivityUseRecord() {
      return activityWriteRepository.deleteActivityUseRecord(this.getActivityUseRecordRelationSDO());
    }
//
//    public Boolean isExistRecord(){
//       return activityQueryRepository.queryActivityUseRecord(this.activityUseRecordRelationSDO);
//    }

    /**
     * 是否参加过该活动
     *
     * @return true: 参过， false,没有参加过
     */
    public Boolean isRepeatUsedActivity() {
        return this.activityUseRecordRelationSDO == null ? false : true;
    }

    /**
     * 获取冗余字段
     * @return
     */
    public String obtainFeatures(){
        return activityUseRecordRelationSDO.getFeatures();
    }

}
